package com.example.pei.ithinklearnt.view.fragments;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.MatchedUserAdapter;
import com.example.pei.ithinklearnt.bean.UserInfo;
import com.example.pei.ithinklearnt.bean.UserSkill;
import com.example.pei.ithinklearnt.present.SkillPresent;
import com.example.pei.ithinklearnt.present.SkillPresentImp;
import com.example.pei.ithinklearnt.utils.LocationUtil;
import com.example.pei.ithinklearnt.utils.SnackbarUtil;
import com.example.pei.ithinklearnt.utils.ToastUtil;
import com.example.pei.ithinklearnt.view.activity.ForgetPassWordActivity;
import com.example.pei.ithinklearnt.view.activity.Login_Activity;
import com.example.pei.ithinklearnt.view.activity.MatchedFriendsActivity;
import com.example.pei.ithinklearnt.view.base.IMutlipleItem;
import com.example.pei.ithinklearnt.view.interfaces.SkillInterface;
import com.skyfishjy.library.RippleBackground;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.BmobUpdateListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.update.BmobUpdateAgent;
import cn.bmob.v3.update.UpdateResponse;
import cn.bmob.v3.update.UpdateStatus;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

import static cn.bmob.v3.Bmob.getApplicationContext;
import static com.example.pei.ithinklearnt.utils.BeanOperationUtil.putBean;
import static com.example.pei.ithinklearnt.utils.PermisionUtil.hasCoarseLocationPermission;

/**
 * Created by Pei on 2017/12/6.
 */

public class SkillFragment extends Fragment implements SkillInterface {

    private SkillPresent skillPresent;

    @BindView(R.id.btn_match)
    Button button_match;








    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.skillmenu_fragment, container, false);
        ButterKnife.bind(this, view);
        skillPresent=new SkillPresentImp(this);





        return view;
    }

    //匹配好友
    @OnClick(R.id.btn_match)
    void matchUser(){
        //rippleBackground.startRippleAnimation();
        skillPresent.matchFriend(new SkillPresentImp.matchFinishedListener() {
            @Override
            public void done(List<UserSkill> bestUsers) {
                //查找完成
                //list转arraylist
                ArrayList arrayList = new ArrayList();
                for(UserSkill u:bestUsers){
                    //出现过一次空的异常，不知道为什么，这么做一个判断吧
                    if(u.getUser()!=null&&u.getUser().getSex()!=null&&u.getUser().getUsername()!=null)
                        arrayList.add(u) ;
                }

                //启动活动
                Intent intent=new Intent(getContext(), MatchedFriendsActivity.class);
                //intent.putParcelableArrayListExtra("result",arrayList);
                intent.putExtra("result",arrayList);
                startActivity(intent);

               //
            }

            @Override
            public void fail(String error) {
                SnackbarUtil.ShowSnackbar(getActivity().findViewById(R.id.coordinatorlayout),
                        " 请在\"个人\"中设置一个想学的技能d(´ω｀*)", "#FFAF70", "#ffffff", 16);
            }
        });
    }

    @Override
    public void stopMatchAnimation() {
      //  rippleBackground.stopRippleAnimation();
    }

    @Override
    public Activity getThisActivity() {
        return getActivity();
    }


}
